# -*- coding: UTF-8 -*-
__author__ = 'chzhu'

from Opener import Opener
from my_parser import Parser
from Logger import Logger
from datetime import datetime
from Config import Config
from Database import Timeline, Database, Uid

class Spider(object):
    def __init__(self):
        self.opener = Opener()
        self.uids = Uid.get_by_id(Config.SPIDER_ID)

    def fetch(self, url):
        return self.opener.urlOpen(url)

    def report(self):
        self.opener.report()
        with Database.dbo.session.begin():
            for uid in self.uids:
                uid.is_available = '1'

    def get_timelines(self, url):
        while True:
            html = self.fetch(url)
            if 'W_icon icon_warnB' in html:
                print 'No post!'
                return
	    if 'W_icon icon_rederrorB' in html:
	        print 'Exceptional User!'
		return
            try:
                if 'W_icon icon_warnB' in html:
                    print 'No post!'
                    return
                elif 'W_icon icon_rederrorB' in html:
                    print 'Exceptional User!'
                    return
                parser = Parser(html)
                parser.parse_timelines()
                with Database.dbo.session.begin():
                    for tl in parser.timelines:
                        Database.dbo.session.merge(Timeline(tl))
                break
            except Exception as e:
                Logger.logger.debug(e)
                continue

    def monitor(self):
        rd = 0
        while True:
            rd += 1
            begin_time = datetime.now()
            for i, uid in enumerate(self.uids):
                uid = uid.uid
                print 'User ID: %s -- Round %d: %d/%d' % (uid, rd, i+1, len(self.uids))
                print 'Updating...'
                url = 'http://www.weibo.com/u/' + uid
                self.get_timelines(url)
                print 'Completed!!!'
                print

            end_time = datetime.now()
            total_seconds = (end_time - begin_time).total_seconds()
            duration_minute = int( total_seconds / Config.seconds_per_minute )
            duration_second = int(round(total_seconds)) % Config.seconds_per_minute
            print '--------------------------------------------------'
            print '--------------------------------------------------'
            print 'Round %d takes %s minutes and %s seconds.' % (rd, duration_minute, duration_second)
            print '--------------------------------------------------'
            print '--------------------------------------------------'
